package com.yxkj.syh.app.huarong.activities.account.login;

import android.os.Bundle;

import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.syh.app.basic.App;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.base.BaseFragment;
import com.syh.app.basic.utils.ActivityManager;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.util.UserManager;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.account.login.pwd.PwdLoginFragment;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityLoginBinding;

import cn.jpush.android.api.JPushInterface;

/**
 * 登录
 */
@Route(path = ArouterPath.LOGIN_ACTIVITY)
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginVM>{


    private BaseFragment mCurrentFragment;
    private PwdLoginFragment mPwdLoginFragment;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected int getViewModelId() {
        return BR.loginVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mPwdLoginFragment = PwdLoginFragment.newInstance();
        switchFragment(mPwdLoginFragment);

        mVDBinding.titleView.setLeftOnClickListener(v -> finish());
    }

    private void switchFragment(final BaseFragment targetFragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            if (mCurrentFragment != null) {
                transaction.hide(mCurrentFragment);
            }
            transaction.add(R.id.flContent, targetFragment, targetFragment.getClass().getSimpleName());
        } else {
            if (targetFragment == mCurrentFragment) return;
            transaction.hide(mCurrentFragment);
            transaction.show(targetFragment);
        }
        mCurrentFragment = targetFragment;
        transaction.commit();
    }

    @Override
    protected void initData() {
        UserManager.getUserManager().clear();//清空用户信息

        JPushInterface.deleteAlias(App.getApplication(),2);
    }

    long last;

    @Override
    public void onBackPressed() {
        long c = System.currentTimeMillis();
        if (c - last > 1000) {
            last = c;
            Tooast.normalInfo("双击退出");
        } else {
            ActivityManager.getInstance().finishAllActivity();
        }
    }
}
